package com.eportal.service.impl;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.NullArgumentException;
import org.springframework.stereotype.Service;

import com.eportal.base.BaseDAO;
import com.eportal.domain.Sysopt;
import com.eportal.domain.TAgent;
import com.eportal.domain.TCommunity;
import com.eportal.domain.TpayWalletlst;
import com.eportal.domain.TuserWallet;
import com.eportal.exception.NotUniqueException;
import com.eportal.service.UserWalletService;
import com.framework.jqgrid.JqGridPager;

@Service("tUserWalletService")
public class UserWalletServiceImpl  implements UserWalletService{

	@Resource(name="dao")
	 private BaseDAO dao;
	
	@Override
	public TuserWallet getOrcreateWallet(String utype, String userid,String cname) throws Exception {
		if(utype==null){
			throw new  NullArgumentException("utype");
		}
		
		if(userid==null){
			throw new  NullArgumentException("userid");
		}
		
		String hql = "from TuserWallet where utype='"+utype+"' and userid='"+userid+"'";
		TuserWallet bean = (TuserWallet)dao.getUniqueFirst(hql);
		if(bean == null){
			bean = new TuserWallet();	
			bean.setAmount(new BigDecimal(0));
			bean.setWtime(new Timestamp(System.currentTimeMillis()));
			bean.setOpttime(new Timestamp(System.currentTimeMillis()));
			bean.setUserid(userid);
			bean.setUtype(utype);
			bean.setCname(cname);
			dao.save(bean);
			return bean;
		}else{
			bean.setCname(cname);
			dao.update(bean);
		}
		return  bean;
	}

	@Override
	public String getAmountName(Integer rlevel, String agtCode, String communityId) throws Exception {
		if(rlevel==0){
			return "掌连云平台";
		}else if(rlevel==1){
			TAgent agent = (TAgent)dao.getUniqueFirst("from TAgent where agtCode='"+agtCode+"'");
			return agent.getAgtName();
		}else if(rlevel==2){
			TCommunity bean = (TCommunity)dao.getUniqueFirst("from TCommunity where sid="+communityId);
			return bean.getCommunityName();
		}
		return null;
	}

	@Override
	public TuserWallet getOrcreateWallet(Integer rlevel, String agtCode, String communityId) throws Exception {
		String utype="";
		String userid=null;
		if(rlevel==0){
			utype="5";
			userid="1";
		}else if(rlevel==1){
			utype="2";
			userid=agtCode;
		}else if(rlevel==2){
			utype="3";
			userid=communityId;
		}else{
			return null;
		}
		String name = getAmountName(rlevel, agtCode, communityId);
		return getOrcreateWallet(utype, userid,name);
		
	}

	/**
	 * 获取钱包的验证电话
	 * 
	 * 平台取admin的电话，其他的取各自的电话
	 */
	@Override
	public String getTelForWallet(Integer rlevel, String agtCode, String communityId) throws Exception {
		if(rlevel==0){
			Sysopt user = (Sysopt)dao.getUniqueFirst("from Sysopt where username='admin' and rlevel=0");
			return user.getTel();
		}else if(rlevel==1){
			TAgent bean = (TAgent)dao.getUniqueFirst("from TAgent where agtCode='"+agtCode+"'");
			return bean.getAgtTel();
		}else if(rlevel==2){
			TCommunity bean = (TCommunity)dao.getUniqueFirst("from TCommunity where sid='"+communityId+"'");
			return bean.getDepartTel(); 
		}
		return null;
	}

	@Override
	public void updatePayPassword(Long wid, String payPassword) {
		dao.updateBySql("update T_User_Wallet set pay_password='"+payPassword+"',password_error_num=0 where wid="+wid);
		
	}

	@Override
	public List<TpayWalletlst> list(JqGridPager page) {
		
		return (List<TpayWalletlst>)dao.listAll("TpayWalletlst", page);
	}
	
	@Override
	public List<Map<String, Object>> listBySql(JqGridPager jqGridPager) {
		String SQL = "select a.*,b.cname from t_pay_walletlst a left join t_user_wallet b on a.s_wid=b.wid ";
	     return dao.query(SQL, jqGridPager);
	}
	
	public List<Map<String, Object>> list4Qrcode(JqGridPager jqGridPager) {
		String SQL = "select a.*,b.cname from t_pay_walletlst a left join t_user_wallet b on a.s_wid=b.wid left join t_thridpay_bill c on a.thrid_bill_id=c.id ";
	     return dao.query(SQL, jqGridPager);
	}
}
